﻿$.kish = {}
$(function () {
    $.kish.UserManagement = {}
    $.extend($.kish.UserManagement, {
        initializing: function () {
            $.kish.UserManagement.ListUseres();
            //member is registering
            var userID = $.Jahanutility.getParameterByName("ID");
            if (userID != "0" && userID != "") {
                $.kish.UserManagement.SetEditPage(userID);
            }

        },

        //************************************//
        SetEditPage: function (inUserID) {
            $.ajax({
                'type': 'POST',
                'url': '../Services/UserManagement.svc/GetUser',
                'data': JSON.stringify({ userID: inUserID }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (data) {
                    if (data['d'] == null) {
                        $.kish.message.getErrorMessage("24");
                    }
                    else {
                        $('.fn_userName').val(data['d'].Name);
                        var Roles = data['d'].Roles;
                        for (var i = 0; i < Roles.length; i++) {
                            //find check box by id which must be same as the id of related role
                            $('input#' + Roles[i]).prop('checked', true);
                        }
                    }

                },
                'error': function (error) { }
            });

            $('a.highslide').click(function () {
                return hs.expand(this)
            });

        },


        //*************************//
        ListUseres: function () {
            $.ajax({
                url: '../Services/UserManagement.svc/GetUsers',
                data: '',
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {

                    if (data['d'] != null) {
                        if (data['d'].length == 0) {
                            //                            $.kish.message.getErrorMessage("15");
                        }
                        else {

                            for (var i = 0; i < data['d'].length; i++) {
                                var date = new Date();
                                if (data['d'][i].DateCreated != null)
                                { date.setTime(data['d'][i].DateCreated.split('(')[1].split('+')[0]); }
                                var x = '<tr id=' + data['d'][i].ID + '> <td> ' + data['d'][i].Name + '</td><td> ' + date + '</td><td><img alt="Edit" title="Edit" class="noBorder pointer fn_edit" src="../Images/ico/edit.gif" /></td><td><img alt="Delete" title="Delete" src="../Images/ico/delete.gif" class="noBorder pointer fn_del" /></td></tr>';
                                $('.fn_users').append(x);
                            }
                            $('.fn_del').click(function () {
                                var thisRow = $(this).closest("tr");
                                $.kish.UserManagement.DeleteUser(thisRow);
                            });
                            $('.fn_edit').click(function () {
                                var ID = $(this).closest("tr").attr('id');
                                window.location.replace("./CreateNewUser.aspx?ID=" + ID);
                            });
                        }

                    }
                }
            });
        },

        //****************************//
        DeleteUser: function (thisRow) {
            var UserID = thisRow.attr('id');
            $.kish.message.getConfirmationMessage(3, function () {
                $.ajax({
                    'type': 'POST',
                    'url': '../Services/UserManagement.svc/DeleteUser',
                    'data': JSON.stringify({ "userID": UserID }),
                    'contentType': 'application/json; charset=utf-8',
                    'dataType': 'json',
                    'success': function (result) {
                        if (result['d'].IsSucceed) {
                            $(thisRow).remove();
                            $.kish.message.getUserMessage(result['d'].Message.Code);
                        }
                        else {
                            $.kish.message.getErrorMessage(result['d'].Message.Code);
                        }
                    },
                    'error': function () {
                        $.kish.message.alertError("Error loading message");
                    }
                });

            }, function () {
                // $this.show();
            });
        }
    });
    $.kish.UserManagement.initializing();
});

